package com.company.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.ServletActionContext;

import com.company.entity.Chanpinfuwu;
import com.company.entity.Projects;
import com.company.service.ChanpinfuwuService;
import com.company.service.ProjectService;

public class ProjectAction extends BaseAction {

	Projects projects;
	ProjectService projectService;
	File upload1;
	File upload2;
	File upload3;
	File upload4;
	String upload1FileName;
	String upload1ContentType;
	String upload2FileName;
	String upload2ContentType;
	String upload3FileName;
	String upload3ContentType;
	String upload4FileName;
	String upload4ContentType;

	public File getUpload1() {
		return upload1;
	}

	public void setUpload1(File upload1) {
		this.upload1 = upload1;
	}

	public File getUpload2() {
		return upload2;
	}

	public void setUpload2(File upload2) {
		this.upload2 = upload2;
	}

	public File getUpload3() {
		return upload3;
	}

	public void setUpload3(File upload3) {
		this.upload3 = upload3;
	}

	public File getUpload4() {
		return upload4;
	}

	public void setUpload4(File upload4) {
		this.upload4 = upload4;
	}

	public String getUpload1FileName() {
		return upload1FileName;
	}

	public void setUpload1FileName(String upload1FileName) {
		this.upload1FileName = upload1FileName;
	}

	public String getUpload1ContentType() {
		return upload1ContentType;
	}

	public void setUpload1ContentType(String upload1ContentType) {
		this.upload1ContentType = upload1ContentType;
	}

	public String getUpload2FileName() {
		return upload2FileName;
	}

	public void setUpload2FileName(String upload2FileName) {
		this.upload2FileName = upload2FileName;
	}

	public String getUpload2ContentType() {
		return upload2ContentType;
	}

	public void setUpload2ContentType(String upload2ContentType) {
		this.upload2ContentType = upload2ContentType;
	}

	public String getUpload3FileName() {
		return upload3FileName;
	}

	public void setUpload3FileName(String upload3FileName) {
		this.upload3FileName = upload3FileName;
	}

	public String getUpload3ContentType() {
		return upload3ContentType;
	}

	public void setUpload3ContentType(String upload3ContentType) {
		this.upload3ContentType = upload3ContentType;
	}

	public String getUpload4FileName() {
		return upload4FileName;
	}

	public void setUpload4FileName(String upload4FileName) {
		this.upload4FileName = upload4FileName;
	}

	public String getUpload4ContentType() {
		return upload4ContentType;
	}

	public void setUpload4ContentType(String upload4ContentType) {
		this.upload4ContentType = upload4ContentType;
	}

	public Projects getProjects() {
		return projects;
	}

	public void setProjects(Projects projects) {
		this.projects = projects;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public String execute() throws Exception {
		// TODO Auto-generated method stub
		setProjects(projectService.query(Integer.valueOf(ServletActionContext
				.getRequest().getParameter("id").toString())));
		if (projects == null ) {
			return ERROR;
		}
		Pattern pattern = Pattern.compile("(\r\n|\r|\n|\n\r)");
		// 正则表达式的匹配一定要是这样，单个替换\r|\n的时候会错误
		Matcher matcher = pattern.matcher(projects.getContent());
		String newString = matcher.replaceAll("<br>");
		projects.setContent(newString);
		ServletActionContext.getRequest().getSession()
				.setAttribute("projects", projects);
		return super.execute();
	}

	public String list() {
		ServletActionContext.getRequest().getSession()
				.setAttribute("list", projectService.querys());
		return SUCCESS;
	}

	public String save() throws FileNotFoundException {
		savePic1();
		savePic2();
		savePic3();
		savePic4();
		projectService.save(projects);
		list();
		return SUCCESS;
	}

	public String delete() {
		projectService.delete(projects);
		list();
		return SUCCESS;
	}

	public String update() {
		if (upload1 == null) {
			projects.setImage1(projectService.query(projects.getId())
					.getImage1());
		} else {
			savePic1();
		}
		if (upload2 == null) {
			projects.setImage2(projectService.query(projects.getId())
					.getImage2());
		} else {
			savePic2();
		}
		if (upload3 == null) {
			projects.setImage3(projectService.query(projects.getId())
					.getImage3());
		} else {
			savePic3();
		}
		if (upload4 == null) {
			projects.setImage4(projectService.query(projects.getId())
					.getImage4());
		} else {
			savePic4();
		}
		projectService.update(projects);
		list();
		return SUCCESS;
	}

	public boolean savePic1() {
		try {
			if (upload1 == null) {
				return false;
			}
			InputStream in = new FileInputStream(upload1);
			String dir = ServletActionContext.getRequest().getRealPath(
					"/images");
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return false;
				}
			}
			upload1FileName = UUID.randomUUID().toString() + "."
					+ upload1ContentType.split("/")[1];
			projects.setImage1(upload1FileName);
			File uploadFile = new File(dir, upload1FileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean savePic2() {
		try {
			if (upload2 == null) {
				return false;
			}
			InputStream in = new FileInputStream(upload2);
			String dir = ServletActionContext.getRequest().getRealPath(
					"/images");
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return false;
				}
			}
			upload2FileName = UUID.randomUUID().toString() + "."
					+ upload2ContentType.split("/")[1];
			projects.setImage2(upload2FileName);
			File uploadFile = new File(dir, upload2FileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean savePic3() {
		try {
			if (upload3 == null) {
				return false;
			}
			InputStream in = new FileInputStream(upload3);
			String dir = ServletActionContext.getRequest().getRealPath(
					"/images");
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return false;
				}
			}
			upload3FileName = UUID.randomUUID().toString() + "."
					+ upload3ContentType.split("/")[1];
			projects.setImage3(upload3FileName);
			File uploadFile = new File(dir, upload3FileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean savePic4() {
		try {
			if (upload4 == null) {
				return false;
			}
			InputStream in = new FileInputStream(upload4);
			String dir = ServletActionContext.getRequest().getRealPath(
					"/images");
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return false;
				}
			}
			upload4FileName = UUID.randomUUID().toString() + "."
					+ upload4ContentType.split("/")[1];
			projects.setImage4(upload4FileName);
			File uploadFile = new File(dir, upload4FileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

}
